package datalookin.network;

import android.util.Log;

import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.whu.zj.MyApplication;
import com.whu.zj.R;
import com.whu.zj.VolleySingleton;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;

import inter.Delegate;

/**
 * 根据水厂编号选择水厂历史信息
 * Created by Administrator on 2016/3/5.
 */
public class GetWatetPlantData {


    Delegate delegate;
    StringRequest stringRequest;
    private String waterPlantCode;//水厂编号
    private String type;
    private String beginTime;
    private String endTime;

    public void setDelegate(Delegate delegate) {
        this.delegate = delegate;
    }

    public GetWatetPlantData(Delegate delegate, String waterPlantCode, String type, String beginTime, String endTime) {
        this.delegate = delegate;
        this.waterPlantCode = waterPlantCode;
        this.type = type;
        this.beginTime = beginTime;
        this.endTime = endTime;
    }

    public void get() {
        String ip = MyApplication.share().getResources().getString(R.string.IP_zj);
        String url = null;
        try {
            url = "http://" + ip + "/GetWatetPlantData?" +
                    "waterPlantCode="+waterPlantCode+"&type="+ URLEncoder.encode(type, "utf-8")+"&beginTime="+beginTime+"&endTime="+endTime;
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }

        stringRequest = new StringRequest(Request.Method.GET, url, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                Log.i("实时数据",response);
                if (delegate != null)
                    delegate.onResponse(response);
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                if (delegate != null)
                    delegate.onErrorResponse(error);
            }
        });
        VolleySingleton.getVolleySingleton(MyApplication.share()).addToRequestQueue(stringRequest);
    }

    public void cancel()
    {
        if (!stringRequest.isCanceled())
            stringRequest.cancel();
    }

}
